package com.wanlian.dao.impl;

import com.wanlian.dao.InviteDao;
import com.wanlian.entity.Invite;
import com.wanlian.model.ResultModel;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class InviteDaoImpl extends GenericDaoImpl<Invite> implements InviteDao {

    public InviteDaoImpl() {
        super(Invite.class);
    }

    @Override
    public ResultModel queryInviteList(int userId) {
        ResultModel resultModel=new ResultModel(true);
        String sql="  select (select username from tb_user where id=it.inviteuserid) as username ,(select headimg from tb_user where id=it.inviteuserid) as headimg," +
                " it.isbinddevice,it.isbindint,it.registertime,it.iscompleted,it.phone from tb_invite it ,tb_user u where u.id=it.userid and u.id="+userId+"";
        StringBuilder sqlWhere=new StringBuilder();
        String[] column = {"username","headimg","isbinddevice", "isbindint", "registertime", "iscompleted", "phone"};
        ArrayList<Object> param = new ArrayList<Object>();
        Page page=new Page(1,99999);
        List<Map<String, Object>> lists = this.queryForListObjectByPage(sql+sqlWhere + " order by registertime desc  ", param.toArray(), page, column);
        resultModel.setObject(lists);
        return resultModel;
    }


    @Override
    public Invite queryUserByDUid(int userId) {
        String hql = String.format("from %s where inviteuserid=? and isbinddevice=false", entityClassName());
        Invite invite = this.queryForObject(hql, new Object[]{userId});
        return invite;
    }

    @Override
    public Invite queryUserByIUid(int userId) {
        String hql = String.format("from %s where inviteuserid=? and isbindint=false", entityClassName());
        Invite invite = this.queryForObject(hql, new Object[]{userId});
        return invite;
    }

    @Override
    public Invite queryUserInvite(int userId) {
        String hql = String.format("from %s where isbinddevice=true and isbindint=true and iscompleted=false and inviteuserid=? ", entityClassName());
        Invite invite = this.queryForObject(hql, new Object[]{userId});
        return invite;
    }
}
